package com.haipumi.servlets.backadmin;



import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.haipumi.beans.JsonModel;
import com.haipumi.beans.haipumi_taskType;
import com.haipumi.biz.TaskTypeBiz;
import com.haipumi.biz.impl.BackAdminBizImpl;
import com.haipumi.biz.impl.TaskTypeBizImpl;
import com.haipumi.servlets.CommonServlet;
import com.haipumi.utils.CommonData;
import com.haipumi.utils.RequestUtil;

public class backTaskTypeServlet extends CommonServlet {

	private static final long serialVersionUID = 53721989605711285L;
	private TaskTypeBiz tt=new TaskTypeBizImpl();
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String op=request.getParameter("op");
		if(op!=null){
			if("findTaskType".equals(op)){
				findTaskTypeOp(request,response);
			}else if("addTaskType".equals(op)){
				addTaskTypeOp(request,response);
			}else if("updateTaskType".equals(op)){
				updateTaskTypeOp(request,response);
			}else if("changeStatus".equals(op)){
				changeStatusOp(request,response);
			}else if("deleteTaskType".equals(op)){
				deleteTaskType(request,response);
			}
		}
	}

	private void deleteTaskType(HttpServletRequest request,
			HttpServletResponse response) {
		String ttid=request.getParameter("ttid");
		boolean flag=false;
		flag=tt.deleteTaskType(ttid);
		JsonModel jm=new JsonModel();
		if(flag){
			jm.setCode(1);
			jm.setMsg("成功删除");
		}else{
			jm.setCode(0);
			jm.setMsg("删除失败,请联系管理员");
		}
		super.outData(response, jm);
	}

	private void changeStatusOp(HttpServletRequest request,
			HttpServletResponse response) {
		haipumi_taskType type=RequestUtil.parseRequest(request, haipumi_taskType.class);
		try {
			type =tt.updateTypeStatus(type);
			List<haipumi_taskType> list=(List<haipumi_taskType>) request.getSession().getServletContext().getAttribute(CommonData.APPLICATIONTASKTYPE);
			for(int i=0;i<list.size();i++){
				if(list.get(i).getTtid()==type.getTtid() ){
					list.get(i).setTtstatus(type.getTtstatus());
					break;
				}
			}
			request.getSession().getServletContext().setAttribute(CommonData.APPLICATIONTASKTYPE, list);
			super.outData(response, type);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void updateTaskTypeOp(HttpServletRequest request,
			HttpServletResponse response) {
		haipumi_taskType type=RequestUtil.parseRequest(request, haipumi_taskType.class);
		
		try {
			type =tt.updateTaskType(type);
			List<haipumi_taskType> list=(List<haipumi_taskType>) request.getSession().getServletContext().getAttribute(CommonData.APPLICATIONTASKTYPE);
			for(int i=0;i<list.size();i++){
				if(list.get(i).getTtid()==type.getTtid() ){
					list.set(i, type);
					break;
				}
			}
			request.getSession().getServletContext().setAttribute(CommonData.APPLICATIONTASKTYPE, list);
			super.outData(response, type);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void addTaskTypeOp(HttpServletRequest request, HttpServletResponse response) {
		haipumi_taskType type=RequestUtil.parseRequest(request, haipumi_taskType.class);
		try {
			type=tt.addTaskType(type);
			List<haipumi_taskType> list=(List<haipumi_taskType>) request.getSession().getServletContext().getAttribute(CommonData.APPLICATIONTASKTYPE);
			list.add(type);
			request.getSession().getServletContext().setAttribute(CommonData.APPLICATIONTASKTYPE, list);
			super.outData(response, type);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void findTaskTypeOp(HttpServletRequest request,
			HttpServletResponse response) throws UnsupportedEncodingException {
		String ttname="";
		if(request.getParameter("ttname")!=null && !"".equals(request.getParameter("ttname")) ){
			ttname=request.getParameter("ttname");
		}
		
		int total=tt.findTaskTypeCount(ttname);
		List<haipumi_taskType> list=tt.findType(ttname,rows, page, sort, order);
		//因为easyui的datagrid 控件要求回传的数据格式必须是这样的 rows:[] ,total:xx
		DataGridModel dgm=new DataGridModel();
		dgm.setRows(list);
		dgm.setTotal(total);
		super.outData(response, dgm);
	}
}
